//
//  KuaiJiePayViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/3.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "KuaiJiePayViewController.h"
#import "JTCSM.h"
#import "ZYRadioButton.h"
#import "PayBankCarListViewController.h"
#import "SurePayViewController.h"
#import "MyBankCardModel.h"
#import "GetInfoClass.h"
#import "WebViewController.h"

static CGFloat viewHeight = 60;
static CGFloat space = 20;
@interface KuaiJiePayViewController ()<PayBankCarListViewControllerDelegate,UITextFieldDelegate>
{
    UIScrollView *backScrollV;
    UILabel *mylabel;
    MyBankCardModel *bankmodel;
    NSString *money;
    NSString *phone;
}
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UIImageView *statusImageView;
@property (nonatomic,strong) UITextField *textfield;
@property (nonatomic,strong) ZYRadioButton *rb1;

@property (nonatomic,strong) ZYRadioButton *rb2;

@end

@implementation KuaiJiePayViewController

- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    
    [[GetInfoClass shareSingle] getinfo];
    [GetInfoClass shareSingle].myblock = ^(UserInfoModel *Model) {
        phone = Model.account;
    };
    backScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERHeight-50)];
    backScrollV.backgroundColor = [UIColor whiteColor];
    backScrollV.userInteractionEnabled = YES;
    backScrollV.bounces = NO;
    backScrollV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:backScrollV];
    [self layoutOtherView];
    [self layoutHeaderView];

//    [self layoutChoicePayWay];
}

- (void)layoutHeaderView{
    CGFloat imageSize = 15;
    CGFloat top = 0;
    [df lineViewWithHeightofTop:top superView:backScrollV spacing:0 color:RGBACOLOR(241, 241, 241, 1) lineHeight:top];
    UIView *backview = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_Y(_textfield)+10, KCONTROLLERWith, viewHeight)];
    backview.userInteractionEnabled = YES;
    [backScrollV addSubview:backview];
    
    self.statusImageView = [[UIImageView alloc]initWithFrame:CGRectMake(KCONTROLLERWith-imageSize-space, top*2+(viewHeight-imageSize)/2, imageSize, imageSize)];
    [self.statusImageView setImage:[UIImage imageNamed:@"myFanhui.png"]];
    [backview addSubview:self.statusImageView];
    self.statusImageView.userInteractionEnabled = YES;
    
    self.nameLabel = [UILabel initWithFrame:CGRectMake(space, top*2, self.statusImageView.frame.origin.x-space, viewHeight) text:@"选择信用卡" font:16 textAlignment:NSTextAlignmentLeft texcolor:[UIColor blackColor] backgroundColor:nil];
    [backview addSubview:self.nameLabel];
    self.nameLabel.userInteractionEnabled = YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(gotobankList) andaddView:backview];

    [df lineViewWithHeightofTop:KVIEW_Y(backview)+6 superView:backScrollV spacing:space color:RGBACOLOR(241, 241, 241, 1) lineHeight:2];
    
    backScrollV.frame = CGRectMake(0, 0, KCONTROLLERWith, KVIEW_Y(_textfield)+10+viewHeight*2+6);
    
    CGFloat btnspace = 20;
    CGFloat btnHeight = 50;
    UIButton *LoginBtn =[[UIButton alloc] initWithFrame:CGRectMake(btnspace, KVIEW_Y(backScrollV)+20 , (KCONTROLLERWith-2*btnspace), btnHeight)];
    LoginBtn.backgroundColor =RGBACOLOR(227, 68, 40, 1);
    [LoginBtn setTitle:@"确认支付" forState:UIControlStateNormal];
    [LoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[df layerWithView:LoginBtn radius:4 width:2 color:RGBACOLOR(172,134,171, 1)];
    [LoginBtn addTarget:self action:@selector(gotoMainV) forControlEvents:UIControlEventTouchUpInside];
    LoginBtn.tag = 10002;
    [self.view addSubview:LoginBtn];
    [df layerWithView:LoginBtn radius:btnHeight/2 width:0 color:nil];

    
}

- (void)gotoMainV{
    
    if (money == nil || [money isEqualToString:@""]) {
        [df showAlertWithTitle:@"请输入金额" message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        return;
    }
    
    if (bankmodel.card_id == nil || [bankmodel.card_id isEqualToString:@""]) {
        [df showAlertWithTitle:@"请选择选择银行卡" message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        return;
    }
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserExpense/create") withDict:@{@"token":KTokenString,@"card_id":bankmodel.card_id,@"total_price":money} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            
            if ([[KResultOfKey(@"data") objectForKey:@"source"] isEqualToString:@""] ||[KResultOfKey(@"data") objectForKey:@"source"]==nil) {
                SurePayViewController*vc = [[SurePayViewController alloc]init];
                vc.nameTitle = @"快捷支付";
                vc.banktel = phone;
                vc.bankname = bankmodel.bank_name;
                vc.bankcard = bankmodel.bank_card;
                vc.money = money;
                vc.client_num = [KResultOfKey(@"data") objectForKey:@"client_num"];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                

                WebViewController *vc = [[WebViewController alloc]init];
                vc.titleName = @"快捷支付";
                vc.isruzhu = @"wangzheguanjia";
                vc.urlStr = [KResultOfKey(@"data") objectForKey:@"source"];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }else{
            [df showAlertWithTitle:KResultOfKey(@"msg") message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }
    }];


}


- (void)gotobankList{
    PayBankCarListViewController *vc = [[PayBankCarListViewController alloc]init];
    vc.nameTitle = @"选择信用卡";
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark PayBankCarListViewControllerDelegate

- (void)getBankInfo:(MyBankCardModel *)model{
    
    NSString *bankcard = [model.bank_card stringByReplacingCharactersInRange:NSMakeRange(0, model.bank_card.length-4) withString:@""];
    self.nameLabel.text = [NSString stringWithFormat:@"%@ (%@)",model.bank_name,bankcard];
    bankmodel = model;
}

- (void)layoutOtherView{
    
    CGFloat changeHeight = 18;

   UILabel *choiceLabel = [UILabel initWithFrame:CGRectMake(space, 25, KCONTROLLERWith-2*space, 20) text:@"支付金额" font:16 textAlignment:NSTextAlignmentLeft texcolor:[UIColor blackColor] backgroundColor:nil];
    [backScrollV addSubview:choiceLabel];
    CGFloat LabelHeight = 50;
    NSString *title = @"¥";
    CGFloat font = 30;
    CGRect rect = [df labelHeightWithContent:title height:LabelHeight font:font];
    
    UILabel *label = [UILabel initWithFrame:CGRectMake(space, KVIEW_Y(choiceLabel)+changeHeight, rect.size.width, rect.size.height) text:title font:font textAlignment:NSTextAlignmentCenter texcolor:RGBACOLOR(89, 89, 89, 1) backgroundColor:nil];
    [backScrollV addSubview:label];
    
    
    self.textfield = [UITextField initWithframe:CGRectZero backgrandColor:nil placeholder:@"输入金额" textAlignment:NSTextAlignmentLeft font:20 texcolor:RGBACOLOR(89, 89, 89, 1)];
    self.textfield.delegate = self;
    [self.textfield addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    [backScrollV addSubview:self.textfield];
    
    CGRect textfieldrect = [df labelHeightWithContent:@"输入金额" width:KCONTROLLERWith-KVIEW_X(label)-space font:20];
    
    self.textfield.frame = CGRectMake(KVIEW_X(label), label.frame.origin.y+(rect.size.height-textfieldrect.size.height)*2/3, KCONTROLLERWith-KVIEW_X(label)-space, textfieldrect.size.height);
    
    [df lineViewWithHeightofTop:KVIEW_Y(self.textfield)+10 superView:backScrollV spacing:space color:RGBACOLOR(241, 241, 241, 1) lineHeight:2];
    
    
}

- (void)valueChanged:(UITextField *)textfield{
    money = textfield.text;
}
- (void)layoutChoicePayWay{
    CGFloat radioBtnsize = 10;
    CGFloat theLabelheight = 40;
    UILabel *UpLabel = [UILabel initWithFrame:CGRectMake(space, KVIEW_Y(mylabel), KCONTROLLERWith-2*space, theLabelheight) text:@"线上支付" font:14 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(89, 89, 89, 1) backgroundColor:nil];
    [backScrollV addSubview:UpLabel];
    UpLabel.userInteractionEnabled = YES;
    backScrollV.userInteractionEnabled = YES;
    //初始化单选按钮控件
    self.rb1 = [[ZYRadioButton alloc] initWithGroupId:@"daikuan" index:0];
    //设置Frame
    self.rb1.frame = CGRectMake(KSCREENWidth-space-22,KVIEW_Y(mylabel)+(theLabelheight-radioBtnsize)/2,radioBtnsize,radioBtnsize);
    //添加到视图容器
    [backScrollV addSubview:self.rb1];
    
    [df lineViewWithHeightofTop:KVIEW_Y(UpLabel) superView:backScrollV spacing:space color:RGBACOLOR(241, 241, 241, 1) lineHeight:1];
    
    UILabel *DownpLabel = [UILabel initWithFrame:CGRectMake(space,  KVIEW_Y(UpLabel), KCONTROLLERWith-2*space, theLabelheight) text:@"线下支付" font:14 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(89, 89, 89, 1) backgroundColor:nil];
    [backScrollV addSubview:DownpLabel];
    DownpLabel.userInteractionEnabled = YES;

    
    //初始化单选按钮控件
    self.rb2 = [[ZYRadioButton alloc] initWithGroupId:@"daikuan" index:1];
    //设置Frame
    self.rb2.frame = CGRectMake(KSCREENWidth-space-22,KVIEW_Y(UpLabel)+(theLabelheight-radioBtnsize)/2,radioBtnsize,radioBtnsize);
    //添加到视图容器
    [backScrollV addSubview:self.rb2];
    [df lineViewWithHeightofTop:KVIEW_Y(DownpLabel) superView:backScrollV spacing:space color:RGBACOLOR(241, 241, 241, 1) lineHeight:1];
    
    UILabel *alertLabel = [UILabel initWithFrame:CGRectMake(space, KVIEW_Y(DownpLabel), KCONTROLLERWith-2*space, theLabelheight) text:@"线上还款需支付手续费0.4% 线下还款需支付手续费0.5% " font:12 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(255, 43, 39, 1) backgroundColor:nil];
    [backScrollV addSubview:alertLabel];


}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     [self.textfield resignFirstResponder];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
